package com.vn.ld.btc.dao.common;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.vn.ld.btc.dao.util.QueryBuilderUtil;
import com.vn.ld.common.domain.PageObjInfo;

public class SearchQueryTemplate implements Serializable {

	private static final long serialVersionUID = -973698092152165456L;

	private String queryString;
	private String countQueryString;
	private String orderString;
	private String whereString;
	private PageObjInfo pageObjInfo;
	private List<SearchItem> listSearchItems;

	public SearchQueryTemplate() {
	}

	public SearchQueryTemplate(String queryString, PageObjInfo pageObjInfo) {
		this.queryString = queryString;
		this.pageObjInfo = pageObjInfo;
	}

	public SearchQueryTemplate(String orderString, String whereString, PageObjInfo pageObjInfo) {
		this.orderString = orderString;
		this.whereString = whereString;
		this.pageObjInfo = pageObjInfo;
	}

	public String getQueryString() {
		return queryString;
	}

	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}

	public String getCountQueryString() {
		return countQueryString;
	}

	public void setCountQueryString(String countQueryString) {
		this.countQueryString = countQueryString;
	}

	public String getOrderString() {
		return orderString;
	}

	public void setOrderString(String orderString) {
		this.orderString = orderString;
	}

	public String getWhereString() {
		return whereString;
	}

	public void setWhereString(String whereString) {
		this.whereString = whereString;
	}

	public PageObjInfo getPageObjInfo() {
		return pageObjInfo;
	}

	public void setPageObjInfo(PageObjInfo pageObjInfo) {
		this.pageObjInfo = pageObjInfo;
	}

	/**
	 * Create queryString
	 * 
	 * @param entiyManager
	 * @return
	 */
	public Query getQuery(EntityManager entiyManager) {
		return QueryBuilderUtil.createQuery(entiyManager, this.queryString, this.orderString, this.whereString, listSearchItems);
	}

	/**
	 * Create count query string
	 * 
	 * @param entityManager
	 * @return
	 */
	public Query getCountQuery(EntityManager entityManager) {
		return QueryBuilderUtil.createCountQuery(entityManager, this.queryString, this.whereString, listSearchItems);
	}

	public void addRestriction(SearchItem searchItem) {
		if (listSearchItems == null) {
			this.listSearchItems = new ArrayList<SearchItem>();
		}
		this.listSearchItems.add(searchItem);
	}

}
